package com.gdones.wms.bus.car.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.gdones.common.core.dto.MyPageBand;
import com.gdones.common.core.dto.MyPager;
import com.gdones.common.core.dto.MyResult;
import com.gdones.common.core.util.MyPinYinUtil;
import com.gdones.wms.bus.car.dto.WmsCars;
import com.gdones.wms.bus.car.mapper.WmsCarsMapper;
import com.gdones.wms.bus.car.service.IWmsCarsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gdones.wms.bus.warehouse.dto.WmsWarehouse;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author GaoJingBo
 * @since 2023-05-18
 */
@Service
public class WmsCarsServiceImpl extends ServiceImpl<WmsCarsMapper, WmsCars> implements IWmsCarsService {

    @Override
    public MyResult pages(WmsCars dto, MyPageBand pageBand) {

        MyResult result = new MyResult();

        WmsCarsMapper wmsCarsMapper = getBaseMapper();

        IPage<WmsCars> pages = wmsCarsMapper.pages(PageDTO.of(pageBand.getPage(), pageBand.getSize()), dto);

        // 根据分页的结果封装返回数据
        MyPager<WmsCars> pager = new MyPager<>();
        pager.setTotal(pages.getTotal());
        pager.setPage(pageBand.getPage());
        pager.setPageSize(pageBand.getSize());
        pager.setRows(pages.getRecords());

        result.setMsg("查询成功！");
        result.setData(pager);

        return result;
    }

    @Override
    public MyResult mylist(WmsCars dto) {

        MyResult result = new MyResult();

        WmsCarsMapper carsMapper = getBaseMapper();

        List<WmsCars> list = carsMapper.myList(dto);

        result.setMsg("查询成功！");
        result.setData(list);

        return result;
    }

    @Override
    public MyResult getByID(String id) {

        MyResult result = new MyResult();

        WmsCarsMapper carsMapper = getBaseMapper();
        WmsCars wmsCars = carsMapper.selectById(id);

        result.setMsg("查询成功！");
        result.setData(wmsCars);

        return result;
    }

    @Override
    public MyResult add(WmsCars dto) {

        MyResult result = new MyResult();

        String code = "CAR-"+ dto.getCarCode()+"-"+new Date().getTime();
        dto.setCode(code);

        WmsCarsMapper carsMapper = getBaseMapper();

        int insert = carsMapper.insert(dto);

        if(insert>0){
            result.setMsg("添加成功！");
        }else{
            result.setMsg("添加失败！");
            result.setCode("9999");
        }

        return result;
    }

    @Override
    public MyResult update(WmsCars dto) {

        MyResult result = new MyResult();

        WmsCarsMapper carsMapper = getBaseMapper();

        int i = carsMapper.updateById(dto);

        if( i>0){
            result.setMsg("修改成功！");
        }else{
            result.setMsg("修改失败！");
            result.setCode("9999");
        }

        return result;
    }

    @Override
    public MyResult deletes(String ids) {

        MyResult result = new MyResult();

        String[] split = ids.split(",");

        List<String> list = Arrays.asList(split);

        WmsCarsMapper carsMapper = getBaseMapper();

        int i = carsMapper.deleteBatchIds(list);

        if( i>0){
            result.setMsg("删除成功！");
        }else{
            result.setMsg("删除失败！");
            result.setCode("9999");
        }

        return result;
    }
}
